package com.javaee.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.javaee.ejb.entity.Employee;
import com.javaee.ejb.session.EmployeeManagerRemote;

/**
 * Servlet implementation class Authentication
 */
public class Authentication extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@EJB
	EmployeeManagerRemote emp;
       
    /**
     * @see HttpServlet#HttpServlet()
     */    public Authentication() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//PrintWriter out = response.getWriter();
		String pass = request.getParameter("inputPassword");
		String login = request.getParameter("inputLogin");
		
		if(pass != null && login != null){
			System.out.println("Login: "+login+" Mdp: "+pass);
			Employee e = emp.getEmployeeFromUsernamePassword(login, pass);
			if(e != null){
				HttpSession s = request.getSession();
				s.setAttribute("currentEmp", e);
				request.getRequestDispatcher("menu.jsp").forward(request, response);
			}
			else{
				request.getRequestDispatcher("authentication.jsp").forward(request, response);
			}			
		}
		else if(pass == null || login==null){
			request.getRequestDispatcher("authentication.jsp").forward(request, response);
		}
	}

}
